package com.zch.user.util;

import java.io.UnsupportedEncodingException;
import java.security.MessageDigest;
import java.security.NoSuchAlgorithmException;

import org.apache.shiro.crypto.hash.Md5Hash;

import com.sun.org.apache.xerces.internal.impl.dv.util.Base64;

import sun.misc.BASE64Encoder;

public class PasswordLock {
	/**
	 * MD5(消息摘要算法)加密,需要导包shiro-core-1.2.3.jar
	 * @param password
	 * @return
	 */
	public static String Md5Lock1(String password){
		String salt = "abcdefg";//加盐
		int count = 2;//散列次数
		Md5Hash md5 = new Md5Hash(password, salt, count);//加密
		//将加密的结果转换为字符串
		String md5Str = md5.toString();
		System.out.println(md5Str);
		return md5Str;
	}
	/**
	 * MD5(消息摘要算法)加密,不需要导包
	 * @param password
	 * @return
	 */
	public static String Md5Lock2(String password){
		MessageDigest md = null;
		BASE64Encoder be = new BASE64Encoder();//BASE64编码方式
		//初始化加密后的密码
		String pwdLock = null;
		try {
			//输入md5就表示md5加密，输入sha(安全哈希算法)就表示sha加密
			md = MessageDigest.getInstance("MD5");
			pwdLock = be.encode(md.digest(password.getBytes("UTF-8")));
		} catch (NoSuchAlgorithmException e) {
			e.printStackTrace();
		} catch (UnsupportedEncodingException e) {
			e.printStackTrace();
		}
		System.out.println(pwdLock);
		return pwdLock;
	}
	/**
	 * Base64(编码集)加密
	 * @param password
	 * @return
	 */
	public static String Base64Lock(String password){
		String encodePwd = Base64.encode(password.getBytes());
		System.out.println(encodePwd);
		return encodePwd;
	}
	/**
	 * Base64(编码集)解密
	 * @param pwdLock
	 * @return
	 */
	public static String Base64Unlock(String pwdLock){
		//先转为字节数组
		byte[] bytes = Base64.decode(pwdLock);
		//再转为字符串
		String password = new String(bytes);
		System.out.println(password);
		return password;
	}
	
	
	public static void main(String[] args) {
		Md5Lock1("123456");
		Md5Lock2("123456");
		Base64Lock("123456");
		Base64Unlock("MTIzNDU2");
	}
}
